// pages/lgsigup/lgsigup.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getcourse_info: app.data.url +'/zhongxue/maoapi/getcourse_info',
    getsign_up: app.data.url +'/zhongxue/maoapi/sign_up',
   kechengs:{name:"2019暑四年级趣味语文入门班",leixing:"语文",shiduan:"06月12日-12月30日",shijian:"周日16：00-18：30",xuexiao:"成都校区",xuefei:"15000"
   },

    items: [{ src: app.data.url +"/uploads/zhongxue/xueshengtou.png", name: "陈雪", sex: "男", age: "女" }, { src: app.data.url +"/uploads/zhongxue/xueshengtou.png", name: "陈雪", sex: "男", age: "女" }],
    course:{},
    student:[],
    xuanzhe:[],
    kong:true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var user = wx.getStorageSync('user');
    var that = this;
    wx.request({
      url: that.data.getcourse_info,
      data:{token:user.token,id:id},
      method:'post',
      success:function(res){
        var data = res.data;
        console.log(data)
        if(data.code==200){
          var xuanzhe = [];
          ea.each(data.data.student,function(i,v){
            if (v.baoming){
              xuanzhe.push(v.id);
            }
          })
          that.setData({ course: data.data.course, student: data.data.student, xuanzhe: xuanzhe});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration:app.data.quanbushijian
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  checkboxChange:function(e){
var value = e.detail.value;
//     var student = this.data.student;
//     var newstudent = [];
//     ea.each(student,function(i,v){
//       // if(v.id)
//     })
//     // var newxuanzhe = [];
//     // ea.each()
console.log(value)
// console.log()
this.setData({xuanzhe:value});
  },
  tijiao:function(){
    var tiaojian = {};
    var kong = this.data.kong;
    if(!kong){
      return
    }
    this.setData({kong:false});
    var students_id = this.data.xuanzhe;
    console.log(students_id);
    if(students_id.length<1){
      this.setData({kong:true});
      wx.showToast({
        title: '请选择小孩,进行报名！！',
        icon:'none',
        duration: app.data.quanbushijian
      })
      return;
    }
    tiaojian.students_id = students_id;
    var user = wx.getStorageSync('user');
    tiaojian.token = user.token;
    tiaojian.course_id = this.data.course.id;
    var that = this;
    wx.showLoading({
      title: '报名中',
    })
    wx.request({
      url: that.data.getsign_up,
      data: tiaojian,
      method: 'POST',
      success: function(res) {
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        // return
        if(data.code==200){
          wx.setStorageSync('user', data.data);

          wx.showModal({
            title: data.msg,
            content: '',
            showCancel:false,
            success:function(){
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000)
            
            }
          })
          // wx.showToast({
          //   title: data.msg,
          //   icon:'none',
          //   duration: app.data.quanbushijian
          // })
          // setTimeout(function(){
            
          // },2300)
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        that.setData({kong:true})
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  tianjia:function(){
    wx.navigateTo({
      url: '/pages/haizixinxi/haizixinxi',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})